var app = getApp();
var constract = require('../../../utils/constract.js');

function getCardList(token, callback) {
  wx.request({
    url: constract.findMyList, //我的名片列表
    header: { token: token },
    success: callback,
    fail: (err) => {
      console.log(err);
    }
  })
}

Page({
  data: {
    optionsListData: [],
    movableViewPosition: {
      x: 0,
      y: 0,
      className: "none",
      data: {}
    },
    scrollPosition: {
      everyOptionCell: 65,
      top: 0,
      scrollTop: 0,
      scrollY: true,
      scrollViewHeight: 1000,
      scrollViewWidth: 375,
      windowViewHeight: 1000,
    },
    selectItemInfo: {
      name: "",
      id: "",
      companyName: "",
      selectIndex: -1,
      selectPosition: 0,
    },
  },
  bindscroll: function (event) {
    var scrollTop = event.detail.scrollTop;
    this.setData({
      'scrollPosition.scrollTop': scrollTop
    })
  },
  getOptionInfo: function (code) {
    for (var i = 0, j = this.data.optionsListData.length; i < j; i++) {
      var optionData = this.data.optionsListData[i];
      if (optionData.id == code) {
        optionData.selectIndex = i;
        return optionData;
      }
    }
    return {};
  },

  getPositionDomByXY: function (potions) {
    var y = potions.y - this.data.scrollPosition.top + this.data.scrollPosition.scrollTop;
    var optionsListData = this.data.optionsListData;
    var everyOptionCell = this.data.scrollPosition.everyOptionCell;
    for (var i = 0, j = optionsListData.length; i < j; i++) {
      if (y >= i * everyOptionCell && y < (i + 1) * everyOptionCell) {
        return optionsListData[i];
      }
    }
    return optionsListData[0];
  },
  draggleTouch: function (event) {
    var touchType = event.type;
    switch (touchType) {
      case "touchstart":
        this.scrollTouchStart(event);
        break;
      case "touchmove":
        this.scrollTouchMove(event);
        break;
      case "touchend":
        this.scrollTouchEnd(event);
        break;
    }
  },
  scrollTouchStart: function (event) {
    // console.log(event);
    var firstTouchPosition = {
      x: event.changedTouches[0].pageX,
      y: event.changedTouches[0].pageY,
    }
    console.log("firstTouchPosition:", firstTouchPosition);
    var domData = this.getPositionDomByXY(firstTouchPosition);
    console.log("domData:", domData);

    //movable-area滑块位置处理
    var movableX = 0;
    var movableY = firstTouchPosition.y - this.data.scrollPosition.top - this.data.scrollPosition.everyOptionCell / 2;

    this.setData({
      movableViewPosition: {
        x: movableX,
        y: movableY,
        className: "",
        data: domData
      }
    })

    var secCode = domData.id;
    var secInfo = this.getOptionInfo(secCode);
    secInfo.selectPosition = event.changedTouches[0].clientY;
    secInfo.selectClass = "dragSelected";

    this.data.optionsListData[secInfo.selectIndex].selectClass = "dragSelected";

    var optionsListData = this.data.optionsListData;

    this.setData({
      'scrollPosition.scrollY': false,
      selectItemInfo: secInfo,
      optionsListData: optionsListData,
      'scrollPosition.selectIndex': secInfo.selectIndex
    })
  },
  scrollTouchMove: function (event) {
    var selectItemInfo = this.data.selectItemInfo;
    var selectPosition = selectItemInfo.selectPosition;
    var moveDistance = event.changedTouches[0].clientY;
    var everyOptionCell = this.data.scrollPosition.everyOptionCell;
    var optionsListData = this.data.optionsListData;
    var selectIndex = selectItemInfo.selectIndex;

    console.log("event.changedTouches:", event.changedTouches);
    //movable-area滑块位置处理
    var movableX = 0;
    var movableY = event.changedTouches[0].pageY - this.data.scrollPosition.top - this.data.scrollPosition.everyOptionCell / 2;


    this.setData({
      movableViewPosition: {
        x: movableX,
        y: movableY,
        className: "",
        data: this.data.movableViewPosition.data
      }
    })

    if (moveDistance - selectPosition > 0 && selectIndex < optionsListData.length - 1) {
      if (optionsListData[selectIndex].id == selectItemInfo.id) {
        optionsListData.splice(selectIndex, 1);
        optionsListData.splice(++selectIndex, 0, selectItemInfo);
        selectPosition += everyOptionCell;
      }
    }

    if (moveDistance - selectPosition < 0 && selectIndex > 0) {
      if (optionsListData[selectIndex].id == selectItemInfo.id) {
        optionsListData.splice(selectIndex, 1);
        optionsListData.splice(--selectIndex, 0, selectItemInfo);
        selectPosition -= everyOptionCell;
      }
    }

    this.setData({
      'selectItemInfo.selectPosition': selectPosition,
      'selectItemInfo.selectIndex': selectIndex,
      optionsListData: optionsListData,
    });
  },

  scrollTouchEnd: function (event) {
    var that = this;
    app.globalData.swiperIdx = 0;
    app.globalData.myCardId = this.data.optionsListData[0].id;

    console.log(event);
    var optionsListData = this.optionsDataTranlate(this.data.optionsListData, "");

    this.setData({
      optionsListData: optionsListData,
      'scrollPosition.scrollY': true,
      'movableViewPosition.className': "none"
    })

    if(wx.showLoading){
      wx.showLoading({
        title: '保存中',
        mask: true,
      })
    }

    app.Verification(function (token) {
      var ids = [];
      that.data.optionsListData.forEach((item) => {
        ids.push(item.id);
      })
      wx.request({
        url: constract.sortById, //查看首页的名片信息
        header: { token: token },
        data: {ids: ids.join(',')},
        complete: ()=>{
          if(wx.hideLoading){
            wx.hideLoading()
          }
        }
      });
    });
  },

  optionsDataTranlate: function (optionsList, selectClass) {
    for (var i = 0, j = optionsList.length; i < j; i++) {
      optionsList[i].selectClass = selectClass;
    }
    return optionsList;
  },

  deleteOption: function(e){
    var that = this;
    var cardId = e.currentTarget.dataset.id
    var systemInfo = wx.getSystemInfoSync();

    app.globalData.swiperIdx = 0;
    app.globalData.myCardId = this.data.optionsListData[0].id;

    wx.showModal({
      title: '提示',
      content	: '确认删除此名片?',
      success: function(res) {
        if (res.confirm) {
          app.Verification(function (token) {
            wx.request({
              url: constract.myDelCard, //查看首页的名片信息
              header: { token: token },
              data: {cardId: cardId},
              success:(res)=>{
                if(+res.data.code === 12000) { //删除成功
                  getCardList(token, (res) => {
                    var optionsList = res.data.data;
                    optionsList = that.optionsDataTranlate(optionsList, "");
            
                    // 开始加载页面
                    var scrollViewHeight = systemInfo.windowHeight;
                    var scrollViewWidth = systemInfo.windowWidth;
            
                    that.setData({
                      optionsListData: optionsList,
                      'scrollPosition.scrollViewWidth': scrollViewWidth,
                      'scrollPosition.scrollViewHeight': scrollViewHeight,
                      'scrollPosition.windowViewHeight': systemInfo.windowHeight,
                    });
            
                  })
                }
              },
              fail: (err)=>{
                console.log(err);
              }
            })
          })
        } else if (res.cancel) {
          
        }
      }
    })
  },

  edit: (e)=>{
    console.log(e);
    var cardId = e.currentTarget.dataset.cardid;
    wx.navigateTo({
      url: '/pages/edit/edit?mycardid='+cardId
    })
  },

  onShow: function () {
    var systemInfo = wx.getSystemInfoSync();

    var that = this;
    app.Verification(function (token) {
      getCardList(token, (res) => {
        var optionsList = res.data.data;
        optionsList = that.optionsDataTranlate(optionsList, "");

        // 开始加载页面
        var scrollViewHeight = systemInfo.windowHeight;
        var scrollViewWidth = systemInfo.windowWidth;

        that.setData({
          optionsListData: optionsList,
          'scrollPosition.scrollViewWidth': scrollViewWidth,
          'scrollPosition.scrollViewHeight': scrollViewHeight,
          'scrollPosition.windowViewHeight': systemInfo.windowHeight,
        });

      })
    })

  },
});